﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


namespace jQueryDotNetLib.Extenders
{
    [ToolboxItem(true)]
    [ToolboxData("<{0}:Resizeable Selector=\"\" runat=server />")]
    public class Resizeable : BaseInteractionExtender
    {
        public Resizeable()
        {
            Interaction = "resizable";
        }

        [Bindable(true)]
        [Category("Client Events")]
        [DefaultValue("")]
        [Localizable(true)]
        public string OnClientResizeStart
        {
            get
            {
                if (ViewState["OnClientResizeStart"] == null) return "";
                return (string)ViewState["OnClientResizeStart"];
            }
            set
            {
                ViewState["OnClientResizeStart"] = value;
            }
        }
        [Bindable(true)]
        [Category("Client Events")]
        [DefaultValue("")]
        [Localizable(true)]
        public string OnClientResize
        {
            get
            {
                if (ViewState["OnClientResize"] == null) return "";
                return (string)ViewState["OnClientResize"];
            }
            set
            {
                ViewState["OnClientResize"] = value;
            }
        }
        [Bindable(true)]
        [Category("Client Events")]
        [DefaultValue("")]
        [Localizable(true)]
        public string OnClientResizeStop
        {
            get
            {
                if (ViewState["OnClientResizeStop"] == null) return "";
                return (string)ViewState["OnClientResizeStop"];
            }
            set
            {
                ViewState["OnClientResizeStop"] = value;
            }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string AlsoResize
        {
            get
            {
                if (ViewState["AlsoResize"] == null) return "";
                return (string)ViewState["AlsoResize"];
            }

            set
            {
                ViewState["AlsoResize"] = value;
            }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public bool Animate
        {
            get
            {
                if (ViewState["Animate"] == null) return false;
                return (bool)ViewState["Animate"];
            }

            set
            {
                ViewState["Animate"] = value;
            }
        }


        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string AnimateDuration
        {
            get
            {
                if (ViewState["AnimateDuration"] == null) return "";
                return (string)ViewState["AnimateDuration"];
            }

            set
            {
                ViewState["AnimateDuration"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string AnimateEasing
        {
            get
            {
                if (ViewState["AnimateEasing"] == null) return "";
                return (string)ViewState["AnimateEasing"];
            }

            set
            {
                ViewState["AnimateEasing"] = value;
            }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public float AspectRatio
        {
            get
            {
                if (ViewState["AspectRatio"] == null) return float.MinValue;
                return (float)ViewState["AspectRatio"];
            }

            set
            {
                ViewState["AspectRatio"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public bool AutoHide
        {
            get
            {
                if (ViewState["AutoHide"] == null) return false;
                return (bool)ViewState["AutoHide"];
            }

            set
            {
                ViewState["AutoHide"] = value;
            }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Cancel
        {
            get
            {
                if (ViewState["Cancel"] == null) return "";
                return (string)ViewState["Cancel"];
            }

            set
            {
                ViewState["Cancel"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Containment
        {
            get
            {
                if (ViewState["Containment"] == null) return "";
                return (string)ViewState["Containment"];
            }

            set
            {
                ViewState["Containment"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public int Delay
        {
            get
            {
                if (ViewState["Delay"] == null) return 0;
                return (int)ViewState["Delay"];
            }

            set
            {
                ViewState["Delay"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public int Distance
        {
            get
            {
                if (ViewState["Distance"] == null) return 1;
                return (int)ViewState["Distance"];
            }

            set
            {
                ViewState["Distance"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public bool Ghost
        {
            get
            {
                if (ViewState["Ghost"] == null) return false;
                return (bool)ViewState["Ghost"];
            }

            set
            {
                ViewState["Ghost"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Grid
        {
            get
            {
                if (ViewState["Grid"] == null) return "";
                return (string)ViewState["Grid"];
            }

            set
            {
                ViewState["Grid"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Handles
        {
            get
            {
                if (ViewState["Handles"] == null) return "";
                return (string)ViewState["Handles"];
            }

            set
            {
                ViewState["Handles"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Helper
        {
            get
            {
                if (ViewState["Helper"] == null) return "";
                return (string)ViewState["Helper"];
            }

            set
            {
                ViewState["Helper"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public int MaxHeight
        {
            get
            {
                if (ViewState["MaxHeight"] == null) return int.MinValue;
                return (int)ViewState["MaxHeight"];
            }

            set
            {
                ViewState["MaxHeight"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public int MaxWidth
        {
            get
            {
                if (ViewState["MaxWidth"] == null) return int.MinValue;
                return (int)ViewState["MaxWidth"];
            }

            set
            {
                ViewState["MaxWidth"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public int MinHeight
        {
            get
            {
                if (ViewState["MinHeight"] == null) return 10;
                return (int)ViewState["MinHeight"];
            }

            set
            {
                ViewState["MinHeight"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public int MinWidth
        {
            get
            {
                if (ViewState["MinWidth"] == null) return 10;
                return (int)ViewState["MinWidth"];
            }

            set
            {
                ViewState["MinWidth"] = value;
            }
        }

        protected override void Render(HtmlTextWriter writer)
        {

            if (AlsoResize != "" && AlsoResize != "false") AddParameter("alsoResize", "'" + AlsoResize + "'");
            if (Animate) AddParameter("animate", "true");
            if (AnimateDuration != "")
            {
                int ani = 0;
                if (int.TryParse(AnimateDuration, out ani))
                {
                    AddParameter("animateDuration", AnimateDuration);
                }
                else
                {
                    AddParameter("animateDuration", "'" + AnimateDuration + "'");
                }
            }
            if (AnimateEasing != "") AddParameter("animateEasing", "'" + AnimateEasing + "'");
            if (AspectRatio != float.MinValue) AddParameter("aspectRatio", AspectRatio.ToString());
            if (AutoHide) AddParameter("autoHide", "true");
            if (Cancel != "") AddParameter("cancel", "'" + Cancel + "'");
            if (Containment != "") AddParameter("containment", "[" + Containment + "]");
            if (Delay != 0) AddParameter("delay", Delay.ToString());
            if (Distance != 1) AddParameter("distance", Distance.ToString());
            if (Ghost) AddParameter("ghost", "true");
            if (Grid != "") AddParameter("grid", "[" + Grid + "]");
            if (Handles != "") AddParameter("handles", "'" + Handles + "'");
            if (Helper != "" && Helper != "false") AddParameter("helper", "'" + Helper + "'");
            if (MaxHeight != int.MinValue) AddParameter("maxHeight", MaxHeight.ToString());
            if (MaxWidth != int.MinValue) AddParameter("maxWidth", MaxWidth.ToString());
            if (MinHeight != 0) AddParameter("minHeight", MinHeight.ToString());
            if (MinWidth != 0) AddParameter("minWidth", MinWidth.ToString());




            AddBinding("resizestart", OnClientResizeStart);
            AddBinding("resize", OnClientResize);
            AddBinding("resizestop", OnClientResizeStop);
            base.Render(writer);

        }
    }
}